package io.alexkozlov.jpointfedagent;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.alexkozlov.jpointfedagent.model.ServiceList;
import io.alexkozlov.jpointfedagent.model.crd.ServiceExport;
import io.alexkozlov.jpointfedagent.model.crd.ServiceImport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class DiscoveryController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DiscoveryController.class);
    private final String discoveryUrl = "http://localhost:8787";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IstioController istioController;

    public void addServiceExport(ServiceExport service) throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String json = new ObjectMapper().writeValueAsString(service);

        HttpEntity<String> request = new HttpEntity<>(json, headers);
        ResponseEntity<String> response = restTemplate.exchange(this.discoveryUrl + "/services/add", HttpMethod.POST, request, String.class);

        LOGGER.info("Add new service:");
        LOGGER.info(response.getBody());
        istioController.createServiceExport(service);
    }

    public void removeServiceExport(ServiceExport service) throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String json = new ObjectMapper().writeValueAsString(service);

        HttpEntity<String> request = new HttpEntity<>(json, headers);
        ResponseEntity<String> response = restTemplate.exchange(this.discoveryUrl + "/services/remove", HttpMethod.POST, request, String.class);

        LOGGER.info("Remove new service:");
        LOGGER.info(response.getBody());
        istioController.deleteServiceExport(service);
    }

    public void addServiceImport(ServiceImport service) {
        LOGGER.info("Add new service:");
        LOGGER.info(service.toString());
        ResponseEntity<ArrayList<ServiceExport>> sResponse =
                restTemplate.exchange(this.discoveryUrl + "/services/get/" + service.getSpec().name,
                        HttpMethod.GET, null, new ParameterizedTypeReference<>() {
                        });
        ArrayList<ServiceExport> services = sResponse.getBody();
        istioController.createServiceImport(service, services);
    }

    public void removeServiceImport(ServiceImport service) throws JsonProcessingException {
        LOGGER.info("Remove service:");
        LOGGER.info(service.toString());
        istioController.deleteServiceImport(service);
    }
}
